package com.jdrx.sw.sewage.service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jdrx.sw.sewage.beans.dto.ProcessSetTO;
import com.jdrx.sw.sewage.beans.dto.StationIdDTO;
import com.jdrx.sw.sewage.beans.dto.StockDevDTO;
import com.jdrx.sw.sewage.beans.vo.BigScreeneFfluentVO;
import com.jdrx.sw.sewage.beans.vo.ProcessunitFPVO;
import com.jdrx.sw.sewage.beans.vo.ProcessunitVO;
import com.jdrx.sw.sewage.beans.vo.StockDevVO;
import com.jdrx.sw.sewage.common.util.LocalDateTimeUtils;
import com.jdrx.sw.sewage.common.util.NumberUtil;
import com.jdrx.sw.sewage.dao.DevStorageDAO;
import com.jdrx.sw.sewage.dao.DeviceTimeDataDAO;
import com.jdrx.sw.sewage.dao.ProcessunitDAO;
import com.jdrx.sw.sewage.dao.StationReportAnalysisDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class ProcessunitServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(ProcessunitServiceImpl.class);

    @Autowired
    private GatherDevServiceImpl gatherDevServiceImpl;
    @Autowired
    private GatherDevPoiServiceImpl gatherDevPoiServiceImpl;
    @Autowired
    private ProcessunitDAO processunitDAO;
    @Autowired
    private DevStorageDAO devStorageDAO;
    @Autowired
    private DeviceTimeDataDAO deviceTimeDataDAO;
    @Autowired
    private StationReportAnalysisDAO stationReportAnalysisDAO;

    public List<ProcessunitVO> selectByStationId(Long stationId) {
        List<ProcessunitVO> processunitVOS = processunitDAO.selectByStationId(stationId);
        processunitVOS.forEach(item -> {
            item.setGatherDevs(gatherDevServiceImpl.selectByStationId(stationId, item.getId()));
        });
        return processunitVOS;
    }

    public List<ProcessunitVO> selectByAll() {
        return processunitDAO.selectByAll();
    }

    public List<StockDevVO> selectByMeIdAndCaIdAndDs(StockDevDTO stockDevDTO) {
        return devStorageDAO.selectByMeIdAndCaIdAndDs(stockDevDTO);
    }

    public List<String> selectDossierSpec(Long materielId, Long categoryId) {
        return devStorageDAO.selectDossierSpec(materielId, categoryId);
    }

    public Map<String, Double> outWater(StationIdDTO dto) {
        AtomicReference<Double> DayCSLJLL = new AtomicReference<>(0d);
        AtomicReference<Double> MonthCSLJLL = new AtomicReference<>(0d);
        BigScreeneFfluentVO po = deviceTimeDataDAO.findRealTimeCrruDayByParams();
        if (Objects.nonNull(po)) {
            JSONObject firstJsonVal = po.getFirstJsonVal();
            JSONObject lastJsonVal = po.getLastJsonVal();
            for (String lastJsonKey : lastJsonVal.keySet()) {
                for (String firstJsonKey : firstJsonVal.keySet())
                    DayCSLJLL.set(lastJsonVal.getDouble(lastJsonKey) - firstJsonVal.getDouble(firstJsonKey));
            }
        }

        BigScreeneFfluentVO mpo = deviceTimeDataDAO.findRealTimeCrrumonthByParams(LocalDateTimeUtils.localDateCURRMONTHToString(new Date()));
        if (Objects.nonNull(mpo)) {
            JSONObject firstJsonVal2 = mpo.getFirstJsonVal();
            JSONObject lastJsonVal2 = mpo.getLastJsonVal();
            for (String lastJsonKey : lastJsonVal2.keySet()) {
                for (String firstJsonKey : firstJsonVal2.keySet())
                    MonthCSLJLL.set(lastJsonVal2.getDouble(lastJsonKey) - firstJsonVal2.getDouble(firstJsonKey));
            }
        }

        HashMap map = Maps.newHashMap();
        Double daycsljll = DayCSLJLL.get();
        Double monthljll = MonthCSLJLL.get();
        map.put("dayLJLL", daycsljll.compareTo(0.0) == 0 ? null : Double.valueOf(NumberUtil.precision(new BigDecimal(daycsljll), 2)));
        map.put("monthLJLL", monthljll.compareTo(0.0) == 0 ? null : Double.valueOf(NumberUtil.precision(new BigDecimal(monthljll), 2)));
        map.put("SSLL", gatherDevPoiServiceImpl.selectCurrOutWater(dto, "SSLL"));
        return map;
    }

    public Map<String, List<ProcessunitFPVO>> processUnitList(StationIdDTO dto) {
        List<ProcessunitFPVO> procs = processunitDAO.findProcUByStationId(dto);
        return procs.stream().collect(Collectors.groupingBy(ProcessunitFPVO::getFactoryPeriod));
    }

    public void stationProcessSet(ProcessSetTO dto) {
        dto.setProcessSet("'" + dto.getProcessSet() + "'");
        processunitDAO.stationProcessSet(dto);
    }

    public Object findProcessSet(Long processId) {
        return processunitDAO.findProcessSet(processId);
    }
}
